#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <iomanip>
#include <windows.h>
#include <conio.h>
#include <algorithm>

using namespace std;

// 提前声明Lidar类，解决交叉引用问题
class Lidar;

// 轮胎类
class Tire {
private:
    string model;
    string size;
public:
    Tire() : model("公路轮"), size("175mm") {}
    Tire(const string& m, const string& s) : model(m), size(s) {}

    void setModel(const string& m) { model = m; }
    void setSize(const string& s) { size = s; }

    void print() const {
        cout << "    型号: " << model << endl;
        cout << "    尺寸: " << size << endl;
    }

    void save(ofstream& outFile) const {
        outFile << "    型号: " << model << "\n";
        outFile << "    尺寸: " << size << "\n";
    }
};

// 底盘类
class Chassis {
private:
    string id;
    string model;
    string wheelbase;
    string track;
    string ground_clearance;
    string turning_radius;
    string drive_type;
    string max_range;
    vector<Tire> tires;
public:
    Chassis() : model("SCOUT MINI"), wheelbase("451mm"), track("490mm"), 
               ground_clearance("115mm"), turning_radius("0m"), 
               drive_type("四轮四驱"), max_range("10KM"), tires(4) {}

    void setId(const string& i) { id = i; }
    void setModel(const string& m) { model = m; }
    void setWheelbase(const string& w) { wheelbase = w; }
    void setTrack(const string& t) { track = t; }
    void setGroundClearance(const string& g) { ground_clearance = g; }
    void setTurningRadius(const string& t) { turning_radius = t; }
    void setDriveType(const string& d) { drive_type = d; }
    void setMaxRange(const string& m) { max_range = m; }
    void setTire(int index, const Tire& tire) { 
        if (index >= 0 && index < tires.size()) {
            tires[index] = tire;
        }
    }

    // 声明函数（定义放在Lidar类之后）
    void executeAction(const Lidar& lidar) const;

    string getId() const { return id; }
    void print() const {
        cout << "--- 底盘 ---" << endl;
        cout << "  编号: " << id << endl;
        cout << "  型号: " << model << endl;
        cout << "  轴距: " << wheelbase << endl;
        cout << "  轮距: " << track << endl;
        cout << "  最小离地间隙: " << ground_clearance << endl;
        cout << "  最小转弯半径: " << turning_radius << endl;
        cout << "  驱动形式: " << drive_type << endl;
        cout << "  最大行程: " << max_range << endl;
        cout << "  轮胎 (" << tires.size() << "个):" << endl;
        for (const auto& tire : tires) {
            tire.print();
        }
    }

    void save(ofstream& outFile) const {
        outFile << "--- 底盘 ---\n";
        outFile << "  编号: " << id << "\n";
        outFile << "  型号: " << model << "\n";
        outFile << "  轴距: " << wheelbase << "\n";
        outFile << "  轮距: " << track << "\n";
        outFile << "  最小离地间隙: " << ground_clearance << "\n";
        outFile << "  最小转弯半径: " << turning_radius << "\n";
        outFile << "  驱动形式: " << drive_type << "\n";
        outFile << "  最大行程: " << max_range << "\n";
        outFile << "  轮胎 (" << tires.size() << "个):\n";
        for (const auto& tire : tires) {
            tire.save(outFile);
        }
    }
};

// 多线激光雷达类
class Lidar {
private:
    string model;
    string channels;
    string range;
    string power_consumption;
    string obstacleStatus;  // 障碍物状态
public:
    Lidar() : model("RS-Helios-16p"), channels("16"), 
             range("100m"), power_consumption("8W"), obstacleStatus("无") {}

    void setModel(const string& m) { model = m; }
    void setChannels(const string& c) { channels = c; }
    void setRange(const string& r) { range = r; }
    void setPowerConsumption(const string& p) { power_consumption = p; }

    // 获取键盘输入设置障碍物状态
    void inputObstacleStatus() {
        int choice;
        cout << "请输入障碍物状态(1-前方, 2-左前方, 3-右前方, 0-无): ";
        cin >> choice;
        
        switch(choice) {
            case 1: obstacleStatus = "前方"; break;
            case 2: obstacleStatus = "左前方"; break;
            case 3: obstacleStatus = "右前方"; break;
            case 0: obstacleStatus = "无"; break;
            default: obstacleStatus = "无效状态";
        }
    }
    
    // 获取障碍物状态
    string getObstacleStatus() const { return obstacleStatus; }

    void print() const {
        cout << "--- 多线激光雷达 ---" << endl;
        cout << "  型号: " << model << endl;
        cout << "  通道数: " << channels << endl;
        cout << "  测试范围: " << range << endl;
        cout << "  功耗: " << power_consumption << endl;
        cout << "  障碍物状态: " << obstacleStatus << endl;
    }

    void save(ofstream& outFile) const {
        outFile << "--- 多线激光雷达 ---\n";
        outFile << "  型号: " << model << "\n";
        outFile << "  通道数: " << channels << "\n";
        outFile << "  测试范围: " << range << "\n";
        outFile << "  功耗: " << power_consumption << "\n";
        outFile << "  障碍物状态: " << obstacleStatus << "\n";
    }
};

// 实现底盘的执行动作方法（在Lidar类定义后实现）
void Chassis::executeAction(const Lidar& lidar) const {
    string status = lidar.getObstacleStatus();
    cout << "\n底盘响应: ";
    if (status == "前方") {
        cout << "后退......" << endl;
    } else if (status == "右前方") {
        cout << "左转......" << endl;
    } else if (status == "左前方") {
        cout << "右转......" << endl;
    } else if (status == "无") {
        cout << "无障碍物，正常行驶" << endl;
    } else {
        cout << "无法识别的障碍物状态" << endl;
    }
}

// AGX套件类
class AgxKit {
private:
    string model;
    string ai_performance;
    string cuda_cores;
    string tensor_cores;
    string memory;
    string storage;
public:
    AgxKit() : model("AGX Xavier"), ai_performance("32 TOPS"), 
              cuda_cores("512"), tensor_cores("64"), 
              memory("32G"), storage("32G") {}

    void setModel(const string& m) { model = m; }
    void setAiPerformance(const string& a) { ai_performance = a; }
    void setCudaCores(const string& c) { cuda_cores = c; }
    void setTensorCores(const string& t) { tensor_cores = t; }
    void setMemory(const string& m) { memory = m; }
    void setStorage(const string& s) { storage = s; }

    void print() const {
        cout << "--- AGX套件 ---" << endl;
        cout << "  型号: " << model << endl;
        cout << "  AI性能: " << ai_performance << endl;
        cout << "  CUDA核心: " << cuda_cores << endl;
        cout << "  Tensor CORE: " << tensor_cores << endl;
        cout << "  显存: " << memory << endl;
        cout << "  存储: " << storage << endl;
    }

    void save(ofstream& outFile) const {
        outFile << "--- AGX套件 ---\n";
        outFile << "  型号: " << model << "\n";
        outFile << "  AI性能: " << ai_performance << "\n";
        outFile << "  CUDA核心: " << cuda_cores << "\n";
        outFile << "  Tensor CORE: " << tensor_cores << "\n";
        outFile << "  显存: " << memory << "\n";
        outFile << "  存储: " << storage << "\n";
    }
};

// 双目摄像头类
class Camera {
private:
    string model;
    string camera_module;
    string rgb_resolution;
    string rgb_fps;
    string fov;
    string depth_fps;
public:
    Camera() : model("RealSense D435i"), camera_module("D430"), 
              rgb_resolution("1920 * 1080"), rgb_fps("30"), 
              fov("87 * 58"), depth_fps("90") {}

    void setModel(const string& m) { model = m; }
    void setCameraModule(const string& c) { camera_module = c; }
    void setRgbResolution(const string& r) { rgb_resolution = r; }
    void setRgbFps(const string& f) { rgb_fps = f; }
    void setFov(const string& f) { fov = f; }
    void setDepthFps(const string& d) { depth_fps = d; }

    void print() const {
        cout << "--- 双目摄像头 ---" << endl;
        cout << "  型号: " << model << endl;
        cout << "  摄像头: " << camera_module << endl;
        cout << "  RGB帧分辨率: " << rgb_resolution << endl;
        cout << "  RGB帧率: " << rgb_fps << endl;
        cout << "  FOV: " << fov << endl;
        cout << "  深度帧率: " << depth_fps << endl;
    }

    void save(ofstream& outFile) const {
        outFile << "--- 双目摄像头 ---\n";
        outFile << "  型号: " << model << "\n";
        outFile << "  摄像头: " << camera_module << "\n";
        outFile << "  RGB帧分辨率: " << rgb_resolution << "\n";
        outFile << "  RGB帧率: " << rgb_fps << "\n";
        outFile << "  FOV: " << fov << "\n";
        outFile << "  深度帧率: " << depth_fps << "\n";
    }
};

// 9轴陀螺仪类
class Gyro {
private:
    string model;
    string manufacturer;
public:
    Gyro() : model("CH110"), manufacturer("NXP") {}

    void setModel(const string& m) { model = m; }
    void setManufacturer(const string& m) { manufacturer = m; }

    void print() const {
        cout << "--- 9轴陀螺仪 ---" << endl;
        cout << "  型号: " << model << endl;
        cout << "  厂家: " << manufacturer << endl;
    }

    void save(ofstream& outFile) const {
        outFile << "--- 9轴陀螺仪 ---\n";
        outFile << "  型号: " << model << "\n";
        outFile << "  厂家: " << manufacturer << "\n";
    }
};

// 液晶显示屏类
class Display {
private:
    string size;
    string model;
public:
    Display() : size("11.6"), model("super") {}

    void setSize(const string& s) { size = s; }
    void setModel(const string& m) { model = m; }

    void print() const {
        cout << "--- 液晶显示屏 ---" << endl;
        cout << "  尺寸: " << size << endl;
        cout << "  型号: " << model << endl;
    }

    void save(ofstream& outFile) const {
        outFile << "--- 液晶显示屏 ---\n";
        outFile << "  尺寸: " << size << "\n";
        outFile << "  型号: " << model << "\n";
    }
};

// 电池模块类
class Battery {
private:
    string params;
    string output_voltage;
    string charge_time;
public:
    Battery() : params("24V/15Ah"), output_voltage("24V"), charge_time("2H") {}

    void setParams(const string& p) { params = p; }
    void setOutputVoltage(const string& o) { output_voltage = o; }
    void setChargeTime(const string& c) { charge_time = c; }

    void print() const {
        cout << "--- 电池模块 ---" << endl;
        cout << "  参数: " << params << endl;
        cout << "  对外供电: " << output_voltage << endl;
        cout << "  充电时长: " << charge_time << endl;
    }

    void save(ofstream& outFile) const {
        outFile << "--- 电池模块 ---\n";
        outFile << "  参数: " << params << "\n";
        outFile << "  对外供电: " << output_voltage << "\n";
        outFile << "  充电时长: " << charge_time << "\n";
    }
};

// 学生类
class Student {
private:
    string id;
    string name;
public:
    Student() : id(""), name("") {}
    Student(const string& i, const string& n) : id(i), name(n) {}

    void setId(const string& i) { id = i; }
    void setName(const string& n) { name = n; }

    void print() const {
        cout << "--- 分配的学生信息 ---" << endl;
        cout << "  学号: " << id << endl;
        cout << "  姓名: " << name << endl;
    }

    void save(ofstream& outFile) const {
        outFile << "--- 分配的学生信息 ---\n";
        outFile << "  学号: " << id << "\n";
        outFile << "  姓名: " << name << "\n";
    }
};

// 智能小车类
class SmartCar {
private:
    string id;
    Chassis chassis;
    AgxKit agx;
    Camera camera;
    Lidar lidar;
    Gyro gyro;
    Display display;
    Battery battery;
    Student assigned_student;
public:
    SmartCar() {}

    void setId(const string& i) { id = i; }
    string getId() const { return id; }  
    void setChassis(const Chassis& c) { chassis = c; }
    void setAgxKit(const AgxKit& a) { agx = a; }
    void setCamera(const Camera& c) { camera = c; }
    void setLidar(const Lidar& l) { lidar = l; }
    void setGyro(const Gyro& g) { gyro = g; }
    void setDisplay(const Display& d) { display = d; }
    void setBattery(const Battery& b) { battery = b; }
    void setAssignedStudent(const Student& s) { assigned_student = s; }

    Chassis getChassis() const { return chassis; }
    AgxKit getAgxKit() const { return agx; }
    Camera getCamera() const { return camera; }
    Lidar& getLidar() { return lidar; }  // 非const版本，用于修改
    const Lidar& getLidar() const { return lidar; }  // const版本，用于读取
    Gyro getGyro() const { return gyro; }
    Display getDisplay() const { return display; }
    Battery getBattery() const { return battery; }
    Student getAssignedStudent() const { return assigned_student; }

    // 更新雷达状态并执行底盘动作
    void updateRadarAndAct() {
        lidar.inputObstacleStatus();
        chassis.executeAction(lidar);
    }

    void print() const {
        cout << "==================== 小车信息 ====================" << endl;
        cout << "小车编号: " << id << endl << endl;
        
        chassis.print();
        cout << endl;
        agx.print();
        cout << endl;
        camera.print();
        cout << endl;
        lidar.print();
        cout << endl;
        gyro.print();
        cout << endl;
        display.print();
        cout << endl;
        battery.print();
        cout << endl;
        assigned_student.print();
        cout << "=================================================" << endl;
    }

    void save(ofstream& outFile) const {
        outFile << "==================== 小车信息 ====================\n";
        outFile << "小车编号: " << id << "\n\n";
        
        chassis.save(outFile);
        outFile << "\n";
        agx.save(outFile);
        outFile << "\n";
        camera.save(outFile);
        outFile << "\n";
        lidar.save(outFile);
        outFile << "\n";
        gyro.save(outFile);
        outFile << "\n";
        display.save(outFile);
        outFile << "\n";
        battery.save(outFile);
        outFile << "\n";
        assigned_student.save(outFile);
        outFile << "=================================================\n\n";
    }
};

// 初始化小车规格（支持手动输入编号）
void initializeCarSpecs(SmartCar& car, int carIndex) {
    string carId, chassisId;
    // 手动输入小车编号
    cout << "\n请输入第 " << carIndex + 1 << " 台小车的编号: ";
    cin >> carId;
    car.setId(carId);
    
    // 手动输入底盘编号
    Chassis chassis;
    cout << "请输入第 " << carIndex + 1 << " 台小车的底盘编号: ";
    cin >> chassisId;
    chassis.setId(chassisId);
    car.setChassis(chassis);
    
    // 其他组件使用默认值
    AgxKit agx;
    Camera camera;
    Lidar lidar;
    Gyro gyro;
    Display display;
    Battery battery;
    
    car.setAgxKit(agx);
    car.setCamera(camera);
    car.setLidar(lidar);
    car.setGyro(gyro);
    car.setDisplay(display);
    car.setBattery(battery);
}

// 功能1: 录入10台小车信息
void inputCarSpecs(vector<SmartCar>& cars, bool& dataEntered) {
    cout << "\n--- 功能1: 录入10台小车信息 ---" << endl;
    if (dataEntered) {
        cout << "警告: 小车信息已存在。是否覆盖? (y/n): ";
        char choice = _getch();
        cout << choice << endl;
        if (tolower(choice) != 'y') {
            cout << "操作已取消。" << endl;
            return;
        }
    }

    cars.clear();
    for (int i = 0; i < 10; ++i) {
        SmartCar car;
        initializeCarSpecs(car, i);
        cars.push_back(car);
    }
    dataEntered = true;
    cout << "成功: 10台小车的硬件规格信息已录入。学生信息暂未分配。" << endl;
}

// 功能2: 分配小车给学生
void assignCarsToStudents(vector<SmartCar>& cars, bool dataEntered, bool& dataAssigned) {
    cout << "\n--- 功能2: 分配小车给学生 ---" << endl;
    if (!dataEntered) {
        cout << "错误: 请先执行功能1录入小车信息！" << endl;
        return;
    }

    cout << "开始为10台小车分配学生..." << endl;
    for (size_t i = 0; i < cars.size(); ++i) {
        cout << "\n[正在分配第 " << i + 1 << "/10 台小车, 编号: " << cars[i].getId() << "]" << endl;
        
        string studentId, studentName;
        cout << "请输入学生学号: ";
        cin >> studentId;
        cout << "请输入学生姓名: ";
        cin.ignore();
        getline(cin, studentName);
        
        Student student(studentId, studentName);
        cars[i].setAssignedStudent(student);
    }
    dataAssigned = true;
    cout << "\n成功: 所有小车已分配给学生。" << endl;
}

// 功能3: 保存信息到文件
void saveToFile(const vector<SmartCar>& cars, const string& filename, bool dataAssigned) {
    cout << "\n--- 功能3: 保存信息到文件 ---" << endl;
    if (!dataAssigned) {
        cout << "错误: 请先执行功能2分配小车给学生！" << endl;
        return;
    }

    ofstream outFile(filename);
    if (!outFile.is_open()) {
        cerr << "错误: 无法打开文件 " << filename << " 进行写入。" << endl;
        return;
    }

    for (const auto& car : cars) {
        car.save(outFile);
    }

    outFile.close();
    cout << "成功: 所有信息已保存到文件: " << filename << endl;
}

// 功能4: 从文件加载并显示信息
void displayFromFile(const string& filename) {
    cout << "\n--- 功能4: 从文件读取并显示信息 ---" << endl;
    ifstream inFile(filename);
    if (!inFile.is_open()) {
        cerr << "错误: 无法打开文件 " << filename << "。请确保您已执行功能3保存了信息。" << endl;
        return;
    }

    vector<string> car_blocks;
    string current_block;
    string line;
    bool in_car_block = false;

    while (getline(inFile, line)) {
        if (line.find("==================== 小车信息 ====================") != string::npos) {
            if (in_car_block) {
                car_blocks.push_back(current_block);
                current_block.clear();
            }
            in_car_block = true;
        }
        
        if (in_car_block) {
            current_block += line + "\n";
        }
    }

    if (in_car_block && !current_block.empty()) {
        car_blocks.push_back(current_block);
    }

    inFile.close();

    if (car_blocks.empty()) {
        cout << "文件中没有找到有效的小车信息。" << endl;
        return;
    }

    int current_car_index = 0;
    char key;

    while (true) {
        system("cls");
        cout << car_blocks[current_car_index];
        cout << "\n--- 控制提示 ---" << endl;
        cout << "按 'n' 显示下一辆, 按 'p' 显示上一辆, 按 'q' 返回主菜单。" << endl;
        cout << "当前: 第 " << current_car_index + 1 << " / " << car_blocks.size() << " 辆" << endl;

        key = _getch();

        if (key == 'n' || key == 'N') {
            if (current_car_index < car_blocks.size() - 1) {
                current_car_index++;
            }
        } else if (key == 'p' || key == 'P') {
            if (current_car_index > 0) {
                current_car_index--;
            }
        } else if (key == 'q' || key == 'Q') {
            break;
        }
    }
}

// 功能5: 保存指定小车的指定模块信息
void saveModuleToFile(const vector<SmartCar>& cars, const string& filename, bool dataEntered) {
    cout << "\n--- 功能5: 保存指定小车模块信息 ---" << endl;
    if (!dataEntered) {
        cout << "错误: 请先执行功能1录入小车信息！" << endl;
        return;
    }

    // 1. 选择小车
    string targetCarId;
    cout << "请输入要操作的小车编号: ";
    cin >> targetCarId;
    auto it = find_if(cars.begin(), cars.end(), [&](const SmartCar& c) {
        return c.getId() == targetCarId;
    });
    if (it == cars.end()) {
        cout << "错误: 未找到编号为 " << targetCarId << " 的小车！" << endl;
        return;
    }
    const SmartCar& targetCar = *it;

    // 2. 选择模块
    cout << "\n请选择要保存的模块（输入数字）:" << endl;
    cout << "1. 底盘  2. AGX套件  3. 双目摄像头  4. 多线激光雷达" << endl;
    cout << "5. 9轴陀螺仪  6. 液晶显示屏  7. 电池模块  8. 学生信息" << endl;
    int moduleChoice;
    cin >> moduleChoice;

    // 3. 打开文件并保存模块
    ofstream outFile(filename, ios::app);
    if (!outFile.is_open()) {
        cerr << "错误: 无法打开文件 " << filename << " 进行写入。" << endl;
        return;
    }

    // 写入模块信息（前缀标注所属小车）
    outFile << "==================== 小车[" << targetCarId << "]模块信息 ====================\n";
    switch (moduleChoice) {
        case 1: targetCar.getChassis().save(outFile); break;
        case 2: targetCar.getAgxKit().save(outFile); break;
        case 3: targetCar.getCamera().save(outFile); break;
        case 4: targetCar.getLidar().save(outFile); break;  // 使用const版本的getLidar()
        case 5: targetCar.getGyro().save(outFile); break;
        case 6: targetCar.getDisplay().save(outFile); break;
        case 7: targetCar.getBattery().save(outFile); break;
        case 8: targetCar.getAssignedStudent().save(outFile); break;
        default: 
            cout << "错误: 无效的模块选择！" << endl;
            outFile.close();
            return;
    }
    outFile << "=================================================\n\n";
    outFile.close();

    cout << "成功: 小车[" << targetCarId << "]的指定模块信息已保存到 " << filename << endl;
}

// 功能6: 显示指定小车的指定模块信息
void displayModule(const vector<SmartCar>& cars, bool dataEntered) {
    cout << "\n--- 功能6: 显示指定小车模块信息 ---" << endl;
    if (!dataEntered) {
        cout << "错误: 请先执行功能1录入小车信息！" << endl;
        return;
    }

    // 1. 选择小车
    string targetCarId;
    cout << "请输入要操作的小车编号: ";
    cin >> targetCarId;
    auto it = find_if(cars.begin(), cars.end(), [&](const SmartCar& c) {
        return c.getId() == targetCarId;
    });
    if (it == cars.end()) {
        cout << "错误: 未找到编号为 " << targetCarId << " 的小车！" << endl;
        return;
    }
    const SmartCar& targetCar = *it;

    // 2. 选择模块并显示
    cout << "\n请选择要显示的模块（输入数字）:" << endl;
    cout << "1. 底盘  2. AGX套件  3. 双目摄像头  4. 多线激光雷达" << endl;
    cout << "5. 9轴陀螺仪  6. 液晶显示屏  7. 电池模块  8. 学生信息" << endl;
    int moduleChoice;
    cin >> moduleChoice;

    system("cls");
    cout << "==================== 小车[" << targetCarId << "]模块信息 ====================\n" << endl;
    switch (moduleChoice) {
        case 1: targetCar.getChassis().print(); break;
        case 2: targetCar.getAgxKit().print(); break;
        case 3: targetCar.getCamera().print(); break;
        case 4: targetCar.getLidar().print(); break;  // 使用const版本的getLidar()
        case 5: targetCar.getGyro().print(); break;
        case 6: targetCar.getDisplay().print(); break;
        case 7: targetCar.getBattery().print(); break;
        case 8: targetCar.getAssignedStudent().print(); break;
        default: cout << "错误: 无效的模块选择！" << endl; return;
    }
    cout << "\n=================================================" << endl;
}

// 功能7: 雷达检测与底盘控制
void radarControl(vector<SmartCar>& cars, bool dataEntered) {
    cout << "\n--- 功能7: 雷达检测与底盘控制 ---" << endl;
    if (!dataEntered) {
        cout << "错误: 请先执行功能1录入小车信息！" << endl;
        return;
    }

    string targetCarId;
    cout << "请输入要操作的小车编号: ";
    cin >> targetCarId;
    
    auto it = find_if(cars.begin(), cars.end(), [&](const SmartCar& c) {
        return c.getId() == targetCarId;
    });
    
    if (it == cars.end()) {
        cout << "错误: 未找到编号为 " << targetCarId << " 的小车！" << endl;
        return;
    }
    
    SmartCar& car = *it;  // 使用非const引用
    car.updateRadarAndAct();
}

int main() {
    SetConsoleOutputCP(CP_UTF8);

    vector<SmartCar> cars;
    const string data_filename = "smart_cars_data.txt";
    
    // 标志位
    bool dataEntered = false;
    bool dataAssigned = false;

    int choice;

    while (true) {
        system("cls"); 
        cout << "========================================" << endl;
        cout << "      智能小车信息管理系统" << endl;
        cout << "========================================" << endl;
        cout << "  1. 录入10台小车信息" << endl;
        cout << "  2. 分配小车给学生" << endl;
        cout << "  3. 保存信息到文件" << endl;
        cout << "  4. 从文件读取并显示信息" << endl;
        cout << "  5. 保存指定小车模块信息" << endl;
        cout << "  6. 显示指定小车模块信息" << endl;
        cout << "  7. 雷达检测与底盘控制" << endl;
        cout << "  0. 退出程序" << endl;
        cout << "========================================" << endl;
        cout << "请输入您的选择: ";
        cin >> choice;

        // 处理输入错误
        if (cin.fail()) {
            cin.clear();
            cin.ignore(10000, '\n');
            choice = -1;
        }

        switch (choice) {
            case 1:
                inputCarSpecs(cars, dataEntered);
                dataAssigned = false;
                break;
            case 2:
                assignCarsToStudents(cars, dataEntered, dataAssigned);
                break;
            case 3:
                saveToFile(cars, data_filename, dataAssigned);
                break;
            case 4:
                displayFromFile(data_filename);
                break;
            case 5:
                saveModuleToFile(cars, data_filename, dataEntered);
                break;
            case 6:
                displayModule(cars, dataEntered);
                break;
            case 7:
                radarControl(cars, dataEntered);
                break;
            case 0:
                cout << "\n感谢使用，程序已退出。" << endl;
                return 0;
            default:
                cout << "\n无效的选择，请输入菜单中列出的数字。" << endl;
                break;
        }

        if (choice != 0) {
            cout << "\n按任意键返回主菜单...";
            _getch();
        }
    }

    return 0;
}